package container

import (
	"context"
	"fmt"
	"grpc.getaway.adminspro.padmins.com/internal/api/utils"
	"grpc.getaway.adminspro.padmins.com/pkg/libs/log"
	"grpc.getaway.adminspro.padmins.com/pkg/libs/request"
	"grpc.getaway.adminspro.padmins.com/pkg/libs/response"
)

type Context struct {
	Request         request.RequestInterface
	Response        response.ResponseInterface
	ResponseMessage *utils.Message
	Auth            *Auth
	handlers        []func(*Context) bool
	Logger          log.Logger
	ctx             context.Context
}

// 返回false表示终止业务逻辑运行，true表示继续业务逻辑运行
func (c *Context) SetMiddleware(middleware func(*Context) bool) {
	c.handlers = append(c.handlers, middleware)
}

// 设置Handler
func (c *Context) SetHandler(handler func(*Context)) {
	c.handlers = append(c.handlers, func(context *Context) bool {
		handler(c)
		return false
	})
}

func (c *Context) SetContext(ctx context.Context) {
	c.ctx = ctx
}

func (c Context) GetContext() context.Context {
	return c.ctx
}

func (c *Context) Handle() {
	for _, handler := range c.handlers {
		b := handler(c)
		if b == false {
			break
		}
	}
}

// 写入数据到Response
func (c *Context) Write(data string) {
	fmt.Fprint(c.Response.GetIoWriter(), data)
}
